package com.itcast.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itcast.mapper.Userlogin;
import com.itcast.pojo.Result;
import com.itcast.pojo.User;
import com.itcast.utils.SMSUtil;
import com.itcast.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @PackageName: com.itcast
 * @ClassName: UserController
 * @Author: Renyz
 * @Date: 2020/2/3 16:51
 * @Description: //TODO
 */

@RestController
@RequestMapping("/User")
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private Userlogin userlogin;

    @RequestMapping("/phone")
    public Result phone(String phone) {

        System.out.println("haha");
        if (phone != null  || !"".equals(phone)) {
            if(phone.length()==11){
                Integer integer = ValidateCodeUtils.generateValidateCode(4);
                String s = String.valueOf(integer);
                try {
                    SMSUtil.sendShortMessage(SMSUtil.VALIDATE_CODE,phone,s);
                    String ss = phone+ "001";
                    ValueOperations valueOperations = redisTemplate.opsForValue();
                    valueOperations.set(ss,s);

                    return new Result(true, "发送成功");
                } catch (ClientException e) {
                    e.printStackTrace();
                    return new Result(false, "发送失败");
                }
            }else {
                return new Result(false, "请输入正确手机号");
            }

        } else {
            return new Result(false, "请输入手机号");
        }
    }


    @PostMapping("/register")
    public Result register(User user,String checkCode){

        if(checkCode != null || !"".equals(checkCode)){
            System.out.println("b3");
            String phone = user.getTelephone();
            String ss = phone+"001";
            String ss1;
            try{
               ss1 = redisTemplate.opsForValue().get(ss).toString();
            }catch (NullPointerException e){
                e.printStackTrace();
                return new Result(false,"验证码错误");
            }

            System.out.println(ss1);
            if(checkCode.equals(ss1)){
                redisTemplate.delete(ss);
                try{
                    userlogin.insert(user);
                    return new Result(true,"注册成功");
                }catch (Exception e){
                    e.printStackTrace();
                    return new Result(false,"注册失败");
                }

            }else {
                return new Result(false,"验证码错误");
            }
        }else {
            return new Result(false,"验证码不能为空");
        }
    }
}
